﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Common;
using System.Reflection;
using AnesSystem.Models;
using System.Windows.Threading;
using AnesSystem.ViewModels;
using AnesSystem.Udp;
using AnesSystem.Model;
using AnesSystem.Services;

namespace AnesSystem.Controls.CommonTables.SC.CDTY
{
    /// <summary>
    /// ShuQIanFangShiCtrs.xaml 的交互逻辑
    /// </summary>
    public partial class ShuQianFangShiCtrl : CustomCommonTableCtrl
    {
        private PatientLisDataModel _patientLisData;
        private DispatcherTimer timer;
        private int i = 0;

        public PatientLisDataModel PatientLisData
        {
            get
            {
                return _patientLisData;
            }

            set
            {
                _patientLisData = value;
            }
        }

        public ShuQianFangShiCtrl()
        {
            InitializeComponent();
            IniteRadioBation();
            this.Loaded += ShuQianFangShiCtrl_Loaded;
            InitalTimer();

        }

        private void ShuQianFangShiCtrl_Loaded(object sender, RoutedEventArgs e)
        {
            FunctionListHeaderCtrlViewModel.Instance.DisplayButton(true, FunctionListButton.打印, FunctionListButton.刷新, FunctionListButton.检验数据);
        }

        private void CustomCheckBoxCtrl_CheckChanged(object sender, RoutedEventArgs e)
        {
            Xxg_y.IsChecked = true;
            Xxg_w.IsChecked = false;
        }

        private void CustomCheckBoxCtrl_CheckChanged_1(object sender, RoutedEventArgs e)
        {
            Fhx_y.IsChecked = true;
            Fhx_w.IsChecked = false;
        }

        private void Mnszy_CheckChanged(object sender, RoutedEventArgs e)
        {
            Mnszy.IsChecked = true;
            Mnszw.IsChecked = false;
        }

        private void Xh_y_CheckChanged(object sender, RoutedEventArgs e)
        {
            Xh_y.IsChecked = true;
            Xh_w.IsChecked = false;
        }

        private void Sjjr_y_CheckChanged(object sender, RoutedEventArgs e)
        {
            Sjjr_y.IsChecked = true;
            Sjjr_w.IsChecked = false;
        }

        private void Nfm_dx_y_CheckChanged(object sender, RoutedEventArgs e)
        {
            Nfm_dx_y.IsChecked = true;
            Nfm_dx_w.IsChecked = false;
        }

        private void Js_y_CheckChanged(object sender, RoutedEventArgs e)
        {
            Js_y.IsChecked = true;
            Js_w.IsChecked = false;
        }

        private void Mzccbw_y_CheckChanged(object sender, RoutedEventArgs e)
        {
            Mzccbw_y.IsChecked = true;
            Mzccbw_w.IsChecked = false;
        }

        private void Yk_y_CheckChanged(object sender, RoutedEventArgs e)
        {
            Yk_y.IsChecked = true;
            Yk_w.IsChecked = false;
        }

        private void Yc_y_CheckChanged(object sender, RoutedEventArgs e)
        {
            Yc_y.IsChecked = true;
            Yc_w.IsChecked = false;
        }

        private void Qdtcd_y_CheckChanged(object sender, RoutedEventArgs e)
        {
            Qdtcd_y.IsChecked = true;
            Qdtcd_w.IsChecked = false;
        }

        private void Mqtsyw_y_CheckChanged(object sender, RoutedEventArgs e)
        {

        }

        private void Jzs_ycjb_y_CheckChanged(object sender, RoutedEventArgs e)
        {
            Jzs_ycjb_y.IsChecked = true;
            Jzs_ycjb_w.IsChecked = false;
        }

        private void Jwmzs_y_CheckChanged(object sender, RoutedEventArgs e)
        {
            Jwmzs_y.IsChecked = true;
            Jwmzs_w.IsChecked = false;
        }

        private void Gms_sss_y_CheckChanged(object sender, RoutedEventArgs e)
        {
            Gms_sss_y.IsChecked = true;
            Gms_sss_w.IsChecked = false;
        }

        private void Cy_xj_ywyl_y_CheckChanged(object sender, RoutedEventArgs e)
        {
            Cy_xj_ywyl_y.IsChecked = true;
            Cy_xj_ywyl_w.IsChecked = false;
        }
        private void IniteRadioBation()
        {
            new RadioButtonHelper().ChangeRadioSatus(Xxg_y, Xxg_w);
            new RadioButtonHelper().ChangeRadioSatus(Fhx_y, Fhx_w);
            new RadioButtonHelper().ChangeRadioSatus(Mnszy, Mnszw);
            new RadioButtonHelper().ChangeRadioSatus(Xh_y, Xh_w);
            new RadioButtonHelper().ChangeRadioSatus(Sjjr_y, Sjjr_w);
            new RadioButtonHelper().ChangeRadioSatus(Xy_y, Xy_w);
            new RadioButtonHelper().ChangeRadioSatus(Nfm_dx_y, Nfm_dx_w);
            new RadioButtonHelper().ChangeRadioSatus(Js_y, Js_w);
            new RadioButtonHelper().ChangeRadioSatus(Ck_y, Ck_w);
            new RadioButtonHelper().ChangeRadioSatus(Cy_xj_ywyl_y, Cy_xj_ywyl_w);
            new RadioButtonHelper().ChangeRadioSatus(Gms_sss_y, Gms_sss_w);
            new RadioButtonHelper().ChangeRadioSatus(Jwmzs_y, Jwmzs_w);
            new RadioButtonHelper().ChangeRadioSatus(Jzs_ycjb_y, Jzs_ycjb_w);
            new RadioButtonHelper().ChangeRadioSatus(Mqtsyw_y, Mqtsyw_w);
            new RadioButtonHelper().ChangeRadioSatus(Qsqk_c, Qsqk_yb, Qsqk_h);
            new RadioButtonHelper().ChangeRadioSatus(Yszt_qx, Yszt_ss, Yszt_hm);
            new RadioButtonHelper().ChangeRadioSatus(Qdtcd_y, Qdtcd_w);
            new RadioButtonHelper().ChangeRadioSatus(Yc_y, Yc_w);
            new RadioButtonHelper().ChangeRadioSatus(Yk_y, Yk_w);
            new RadioButtonHelper().ChangeRadioSatus(Mzccbw_y, Mzccbw_w);
            new RadioButtonHelper().ChangeRadioSatus(XbXp_y, XbXp_w);
            new RadioButtonHelper().ChangeRadioSatus(Xdt_y, Xdt_w);
            new RadioButtonHelper().ChangeRadioSatus(Asa1, Asa2, Asa3, Asa4, Asa5);
            new RadioButtonHelper().ChangeRadioSatus(Bw_s, Bw_f);


        }
        private void InitalTimer()
        {
            timer = new DispatcherTimer();
            timer.Interval = new TimeSpan(0, 0, 1);
            timer.Tick += Timer_Tick;
        }
        private void Timer_Tick(object sender, EventArgs e)
        {
            if (i < 30)
            {
                PatientLisData = Util.ExtensionMethods.GetLisData();
                if (PatientLisData != null && PatientLisData.Result == true)
                {
                    FillLisData();
                    MainPageCommonViewModel.Instance.WaitCtrlVisibility = Visibility.Collapsed;
                    i = 0;
                    timer.Stop();
                    return;
                }
                i++;
            }
            else
            {
                MainPageCommonViewModel.Instance.WaitCtrlVisibility = Visibility.Collapsed;
                i = 0;
                timer.Stop();
                MessageBoxCommon.ShowDialog("获取数据完成，若未获取到检验数据，请稍等几分钟重试！！！", MessageBoxCommon.OK);
            }
        }

        public override void GetLisData()
        {
            string Zyh = DataManager.Instance.CurrentPatientLink.Patient.Zyh;
            if (Zyh == null || "".Equals(Zyh))
            {
                MessageBoxCommon.ShowDialog("请输入住院号重试！！！", MessageBoxCommon.OK);
                return;
            }

            try
            {
                MainPageCommonViewModel.Instance.WaitCtrlVisibility = Visibility.Visible;

                try
                {
                    PatientLisData = Util.ExtensionMethods.GetLisData();
                }
                catch (Exception ex)
                {
                    LogHelper.Error(this.GetType(), ex);
                }
                if (PatientLisData != null)
                {
                    //PatientLisDatas.Result = false;
                    //PatientLisDataService.Instance.AddUpdate(PatientLisDatas);
                    FillLisData();
                    MainPageCommonViewModel.Instance.WaitCtrlVisibility = Visibility.Collapsed;
                    return;
                }
                UdpModel udpModel = new UdpModel();
                udpModel.PatientId = DataManager.Instance.CurrentPatientLink.Id.ToString();
                udpModel.Zyh = Zyh;
                udpModel.UpdateDate = DateTime.Now.ToString();
                udpModel.AccountID = DataManager.Instance.CurrentPatientLink.Patient.Brxm;
                UdpMangager.Instance.SendUdpServerMsg(ServiceSign.GETLISDATA, udpModel);
                timer.Start();//执行查询方法的线程  
            }
            catch (Exception ex)
            {
                Common.LogHelper.Error(this.GetType(), ex);
            }
        }

        private void FillLisData()
        {
            JianYanModel jianyian = PatientLisData.JianYan;
            AnesSystem.Models.Model.Hospital.SC.CDTY.PatientLinkModel patient = DataManager.Instance.CurrentPatientLink as AnesSystem.Models.Model.Hospital.SC.CDTY.PatientLinkModel;
            patient.ShuQianFangShi.HbHCT = jianyian.Hb;
            patient.ShuQianFangShi.WBC = jianyian.Wbc;
            patient.ShuQianFangShi.PLT = jianyian.Plt;
            patient.ShuQianFangShi.Kj = jianyian.K;
            patient.ShuQianFangShi.Naj = jianyian.NA;
            patient.ShuQianFangShi.Cij = jianyian.CL;
            patient.ShuQianFangShi.GLU = jianyian.GLU;
            patient.ShuQianFangShi.ALT = jianyian.ALT;
            patient.ShuQianFangShi.AST = jianyian.AST;
            patient.ShuQianFangShi.BUN = jianyian.BUN;
            patient.ShuQianFangShi.Cr = jianyian.Cr;
            patient.ShuQianFangShi.PT = jianyian.PT;
            patient.ShuQianFangShi.APTT = jianyian.APTT;
            patient.ShuQianFangShi.DDC = jianyian.DDC;

            patient.ShuQianFangShi.Sqsx_azb = jianyian.HIV;
            patient.ShuQianFangShi.Sqsx_bg = jianyian.HCV;
            patient.ShuQianFangShi.Sqsx_yg = jianyian.Hbsag;
            patient.ShuQianFangShi.Sqsx_md = jianyian.Tpab;
            PatientLinkService.Instance.AddUpdate(patient);
        }

        //private void Date_GotFocus(object sender, RoutedEventArgs e)
        //{
        //    CusDatePickerWin cusDatePicker = new CusDatePickerWin();
        //    cusDatePicker.SelectedDateTime = DateTime.Now;
        //    cusDatePicker.ShowDialog();
        //    if (cusDatePicker.Result)
        //    {
        //        Date.Value = cusDatePicker.SelectedDateTime.ToString(DateFormat.DATE_FORMAT_STR_YMD_CN);//xxxx年xx月xx日
        //    }
        //}
    }
}
